#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
const int MAXN=40;
int n,a[MAXN],f[MAXN][MAXN],root[MAXN][MAXN];
void dfs(int l,int r)
{
    if(l>r)return;
    printf("%d ",root[l][r]);
    dfs(l,root[l][r]-1);
    dfs(root[l][r]+1,r);
    return;
}
int main()
{
    n=read();
    for(int i=0;i<=n;++i)
        for(int j=0;j<=n;++j)
            f[i][j]=1;
    for(int i=1;i<=n;++i)
    {
        a[i]=read();
        f[i][i]=a[i];
        root[i][i]=i;
    }
    for(int k=2;k<=n;++k)
    {
        for(int l=1;l<=n-k+1;++l)
        {
            int r=l+k-1;
            for(int mid=l;mid<=r;++mid)
            {
                if(f[l][r]<f[l][mid-1]*f[mid+1][r]+a[mid])
                {
                    f[l][r]=f[l][mid-1]*f[mid+1][r]+a[mid];
                    root[l][r]=mid;
                }
            }
        }
    }
    printf("%d\n",f[1][n]);
    dfs(1,n);
    puts("");
    return 0;
}
